package com.e0box.game.sound
{
	import flash.events.Event;
	
	import com.e0box.game.event.SoundEvent;
	
	[Event(name = "onPlayComplete",type = "net.e0box.game.theAbysm.event.SoundEvent")]
	[Event(name = "onLoopFinish",type = "net.e0box.game.theAbysm.event.SoundEvent")]
	[Event(name = "onLooping",type = "net.e0box.game.theAbysm.event.SoundEvent")]
	public class SubSfx extends Sfx
	{
		public var key:String;
		private var _currentLoop:uint;
		public function SubSfx(source:Class, complete:Function=null)
		{
			
			super(source, complete);
		}
		/**
		 * loop+,loopCount can be setted
		 * @param loopCount
		 * @param volume
		 * @param pan
		 * 
		 */		
		public function loopIn(loopCount:uint = 0,volume:Number = 1,pan:Number = 0):void{
			_currentLoop = loopCount;
			loop(volume,pan);
		}
		/**
		 * override super.onComplete to realize patch Event
		 * @param e
		 * 
		 */		
		override protected function onComplete(e:Event=null):void{
			var patchEvt:SoundEvent;
			if(_looping){
				if(_currentLoop != 0){ 
					_looping = !(--_currentLoop == 0);
				}
				if(_looping){
					patchEvt = new SoundEvent(SoundEvent.ON_LOOPING);
				}else{
					patchEvt = new SoundEvent(SoundEvent.ON_PLAY_COMPLETE);
				}
			}else{
				patchEvt = new SoundEvent(SoundEvent.ON_PLAY_COMPLETE);
			}
			patchEvt.key = key;
			
			super.onComplete(e);
			dispatchEvent(patchEvt);
		}
	}
}